import Handsontable from "handsontable";
import { undoManager, checkSelection } from "../util/Utils.js";

export default {
  buildQrcode (designer) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const hot = context.hot;
    const selected = hot.getSelected()[0];
    const startRow = selected[0];
    const startCol = selected[1];
    const endRow = selected[2];
    const endCol = selected[3];
    let cellDef = context.getCell(startRow, startCol);
    let oldValue = cellDef.value;
    let oldCellData = hot.getDataAtCell(startRow, startCol);
    hot.setDataAtCell(startRow, startCol, "");
    let td = hot.getCell(startRow, startCol);
    let width = this._buildWidth(startCol, td.colSpan, hot);
    let height = this._buildHeight(startRow, td.rowSpan, hot);
    cellDef.value = {
      width,
      height,
      type: "zxing",
      category: "qrcode",
      source: "text",
      data: ""
    };
    hot.render();
    context.isModify = true;
    Handsontable.hooks.run(hot, "afterSelectionEnd", startRow, startCol, endRow, endCol);
    undoManager.add({
      redo: () => {
        cellDef = context.getCell(startRow, startCol);
        oldValue = cellDef.value;
        oldCellData = hot.getDataAtCell(startRow, startCol);
        hot.setDataAtCell(startRow, startCol, "");
        td = hot.getCell(startRow, startCol);
        width = this._buildWidth(startCol, td.colSpan, hot);
        height = this._buildHeight(startRow, td.rowSpan, hot);
        cellDef.value = {
          width,
          height,
          type: "zxing",
          category: "qrcode",
          source: "text",
          data: ""
        };
        hot.render();
        context.isModify = true;
        Handsontable.hooks.run(hot, "afterSelectionEnd", startRow, startCol, endRow, endCol);
      },
      undo: function () {
        cellDef = context.getCell(startRow, startCol);
        cellDef.value = oldValue;
        hot.setDataAtCell(startRow, startCol, oldCellData);
        hot.render();
        context.isModify = true;
        Handsontable.hooks.run(hot, "afterSelectionEnd", startRow, startCol, endRow, endCol);
      }
    });
  },
  builderBarcode (designer) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const hot = context.hot;
    const selected = hot.getSelected()[0];
    const startRow = selected[0];
    const startCol = selected[1];
    const endRow = selected[2];
    const endCol = selected[3];

    let cellDef = context.getCell(startRow, startCol);
    let oldValue = cellDef.value;
    let oldCellData = hot.getDataAtCell(startRow, startCol);
    hot.setDataAtCell(startRow, startCol, "");
    let td = hot.getCell(startRow, startCol);
    let width = this._buildWidth(startCol, td.colSpan, hot);
    let height = this._buildHeight(startRow, td.rowSpan, hot);
    cellDef.value = {
      width,
      height,
      type: "zxing",
      category: "barcode",
      source: "text",
      format: "CODE_128",
      data: ""
    };
    hot.render();
    context.isModify = true;
    Handsontable.hooks.run(hot, "afterSelectionEnd", startRow, startCol, endRow, endCol);
    undoManager.add({
      redo: () => {
        cellDef = context.getCell(startRow, startCol);
        oldValue = cellDef.value;
        oldCellData = hot.getDataAtCell(startRow, startCol);
        hot.setDataAtCell(startRow, startCol, "");
        td = hot.getCell(startRow, startCol);
        width = this._buildWidth(startCol, td.colSpan, hot);
        height = this._buildHeight(startRow, td.rowSpan, hot);
        cellDef.value = {
          width,
          height,
          type: "zxing",
          category: "barcode",
          source: "text",
          format: "CODE_128",
          data: ""
        };
        hot.render();
        context.isModify = true;
        Handsontable.hooks.run(hot, "afterSelectionEnd", startRow, startCol, endRow, endCol);
      },
      undo: () => {
        cellDef = context.getCell(startRow, startCol);
        cellDef.value = oldValue;
        hot.setDataAtCell(startRow, startCol, oldCellData);
        hot.render();
        context.isModify = true;
        Handsontable.hooks.run(hot, "afterSelectionEnd", startRow, startCol, endRow, endCol);
      }
    });
  },
  _buildWidth (colIndex, colspan, hot) {
    let width = hot.getColWidth(colIndex) - 3;
    if (!colspan || colspan < 2) {
      return width;
    }
    const start = colIndex + 1;
    const end = colIndex + colspan;
    for (let i = start; i < end; i++) {
      width += hot.getColWidth(i);
    }
    return width;
  },

  _buildHeight (rowIndex, rowspan, hot) {
    let height = hot.getRowHeight(rowIndex) - 3;
    if (!rowspan || rowspan < 2) {
      return height;
    }
    const start = rowIndex + 1;
    const end = rowIndex + rowspan;
    for (let i = start; i < end; i++) {
      height += hot.getRowHeight(i);
    }
    return height;
  }
};
